package com.phkj.qdh.ui.fragment.failover

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.phkj.qdh.R
import com.phkj.qdh.databinding.FragmentFailoverSelectBinding
import com.phkj.qdh.ui.adapter.FailoverTypeSelectAdapter
import com.phkj.qdh.ui.dialog.FileoverTypedetailDialog
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 容错激励选择类型
 */
class FailoverSelectFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private val failoverTypeSelectAdapter by lazy { FailoverTypeSelectAdapter() }

    private var _binding: FragmentFailoverSelectBinding? = null
    private lateinit var vm: FailoverVM
    private val binding get() = _binding!!
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentFailoverSelectBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getFailoverVMProvider())[FailoverVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            rvLawType.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = failoverTypeSelectAdapter
                addItemDecoration(MyItemDecoration(32))
            }
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack("fail", FragmentManager.POP_BACK_STACK_INCLUSIVE)
            }
        }
        failoverTypeSelectAdapter.addOnItemChildClickListener(R.id.tv_detail){adapter,_,pos->
            val dialog = FileoverTypedetailDialog(requireContext())
            dialog.setTitle(adapter.getItem(pos)!!.name)
            dialog.setContent(adapter.getItem(pos)!!.content)
            XPopup.Builder(requireContext()).asCustom(dialog).show()
        }

        failoverTypeSelectAdapter.addOnItemChildClickListener(R.id.iv_bg){adapter,_,pos->
            parentFragmentManager.commit {
                addToBackStack("fail")
                replace(id,FailoverSubmitFragment.newInstance(adapter.getItem(pos)!!,""))
                setReorderingAllowed(true)
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.failoverTypeList.collectLatest {
                failoverTypeSelectAdapter.submitList(it)
            }
        }

        vm.getFailoverTypeList(hashMapOf<String,Any?>())
    }

    companion object {
        /**
         *
         *
         */
        @JvmStatic
        fun newInstance(param1: String, param2: String) = FailoverSelectFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}